﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;

namespace Web.Helper
{
    public static class RenderToStringExtension
    {
        // Methods
        public static string RenderPartialViewToString(this Controller controller, string viewName = null, object model = null)
        {
            if (string.IsNullOrEmpty(viewName))
            {
                viewName = controller.ControllerContext.RouteData.GetRequiredString("action");
            }
            controller.ViewData.Model = model;
            using (StringWriter writer = new StringWriter())
            {
                ViewEngineResult result = ViewEngines.Engines.FindPartialView(controller.ControllerContext, viewName);
                ViewContext viewContext = new ViewContext(controller.ControllerContext, result.View, controller.ViewData, controller.TempData, writer);
                result.View.Render(viewContext, writer);
                return writer.GetStringBuilder().ToString();
            }
        }

        public static string RenderToString(this PartialViewResult partialViewResult, ControllerContext controllerContext)
        {
            if (string.IsNullOrEmpty(partialViewResult.ViewName))
            {
                throw new Exception("Lo sentimos, ha ocurrido un error en la aplicación, inténtelo mas tarde.");
            }
            using (StringWriter writer = new StringWriter())
            {
                if (partialViewResult.View == null)
                {
                    ViewEngineResult result = partialViewResult.ViewEngineCollection.FindPartialView(controllerContext, partialViewResult.ViewName);
                    partialViewResult.View = result.View;
                }
                ViewContext viewContext = new ViewContext(controllerContext, partialViewResult.View, partialViewResult.ViewData, partialViewResult.TempData, writer);
                partialViewResult.View.Render(viewContext, writer);
                return writer.ToString();
            }
        }

        public static string RenderToString(this ViewResult viewResult, ControllerContext controllerContext, string masterName = null)
        {
            using (StringWriter writer = new StringWriter())
            {
                if (viewResult.View == null)
                {
                    ViewEngineResult result = viewResult.ViewEngineCollection.FindView(controllerContext, viewResult.ViewName, masterName);
                    viewResult.View = result.View;
                }
                ViewContext viewContext = new ViewContext(controllerContext, viewResult.View, viewResult.ViewData, viewResult.TempData, writer);
                viewResult.View.Render(viewContext, writer);
                return writer.ToString();
            }
        }

        public static string RenderViewToString(this Controller controller, string viewName = null, object model = null, string masterName = null)
        {
            if (string.IsNullOrEmpty(viewName))
            {
                viewName = controller.ControllerContext.RouteData.GetRequiredString("action");
            }
            controller.ViewData.Model = model;
            using (StringWriter writer = new StringWriter())
            {
                ViewEngineResult result = ViewEngines.Engines.FindView(controller.ControllerContext, viewName, masterName);
                ViewContext viewContext = new ViewContext(controller.ControllerContext, result.View, controller.ViewData, controller.TempData, writer);
                result.View.Render(viewContext, writer);
                return writer.GetStringBuilder().ToString();
            }
        }
    }
}
